package org.example.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.example.bean.Product;
import org.example.feign.OtherFeignClient;
import org.example.feign.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class OrderService {


    public Product getProduct(String id) {
        Product product = new Product();
        product.setId(id);
        product.setName("product-" + id);
        product.setPrice(new BigDecimal(100));
        product.setNum(1);
        return product;
    }

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    public Product getProductRemote01(String id) {

        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance serviceInstance = instances.get(0);
        String url ="http://"+ serviceInstance.getHost()+":"+serviceInstance.getPort()+"/getProduct/"+id;
        log.info("访问地址："+url);
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    @Autowired
    LoadBalancerClient loadBalancerClient;

    public Product getProductRemote02WithLoad(String id) {

        ServiceInstance serviceInstance = loadBalancerClient.choose("service-product");
        String url ="http://"+ serviceInstance.getHost()+":"+serviceInstance.getPort()+"/getProduct/"+id;
        log.info("访问地址："+url);
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    public Product getProductRemote03WithLoad(String id) {

        String url ="http://service-product/getProduct/"+id;
        log.info("访问地址："+url);
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }


    @Autowired
    private ProductFeignClient productFeignClient;
    public Product getProductRemote04WithFeign(String id) {
        Product product = productFeignClient.getProduct(id, "token01value");
        return product;
    }

    @Autowired
    private OtherFeignClient otherFeignClient;
    public String getOtherInfo(String id) {
        return otherFeignClient.getOther(id);
    }

    public Object timeOutTest() {
        return productFeignClient.getProductList();
    }

    public Object fallbackTest() {
        return productFeignClient.getFallbackTest();
    }

    @SentinelResource(value = "sentinelProductTest")
    public Product sentinelTest(String id) {
        Product product = productFeignClient.getProduct(id, "token01value");
        return product;
    }

    @SentinelResource(value = "sentinelResourceFallbackTest",blockHandler = "sentinelResourceFallback")
    public Product sentinelResourceFallbackTest(String id) {
        Product product = productFeignClient.getProduct(id, "token01value");
        return product;
    }

    public Product sentinelResourceFallback(String id, BlockException e) {
        log.info("sentinelResourceFallback兜底回调");
        Product product = new Product();
        product.setId("0");
        product.setName("sentinelResourceFallback兜底回调"+e.getMessage());
        return product;
    }

}
